local game = require "club.threecard.blackjack"

local actions = game.ui.element()

actions.store_watchers = 
{
    ingame = 
    {
        ["阶段变更"] = function(self, store, state, args, initialized)
            if initialized then
                self.insure_root:SetActive(false);
                return;
            end


            if state.phase == state.PHASE.INSURE and state.self_playing then
                self:on_insure(state.countdown);
            else
                self.insure_root:SetActive(false);
            end

            if state.phase ~= state.PHASE.PLAYER then
                self.action_root:SetActive(false);
            end
        end,

        ["玩家回合"] = function(self, store, state, args, initialized)
            if initialized then
                self.action_root:SetActive(false);
                return;
            end

            if state.current_player == game.kbe.player() then
                self:on_actions(state.action_ids);
            else
                self.action_root:SetActive(false);
            end
        end
    }
}

function actions:ctor()
    self.res = "club.threecard.blackjack/ui/ingame.b:actions"
end

function actions:on_injected(bind)
    bind("button.click", self.action_1, self.on_action_1);
    bind("button.click", self.action_2, self.on_action_2);
    bind("button.click", self.action_3, self.on_action_3);
    bind("button.click", self.action_4, self.on_action_4);

    bind("button.click", self.btn_insure, self.on_click_insure);
    bind("button.click", self.btn_not_insure, self.on_click_not_insure)

    game.utility.button_style.highlight_scale(self.action_1);
    game.utility.button_style.highlight_scale(self.action_2);
    game.utility.button_style.highlight_scale(self.action_3);
    game.utility.button_style.highlight_scale(self.action_4);
    game.utility.button_style.highlight_scale(self.btn_insure);
    game.utility.button_style.highlight_scale(self.btn_not_insure);
end

-- 要牌
function actions:on_action_1()
    game.kbe.player():hit();
end

-- 分牌
function actions:on_action_2()
    game.kbe.player():split();
end

-- 停牌
function actions:on_action_3()
    game.kbe.player():stand();
end

-- 翻倍
function actions:on_action_4()
    game.kbe.player():double_down();
end

-- 买保险
function actions:on_click_insure()
    local need_chip = game.store.ingame.state.bet_value * 0.5;
    if need_chip > game.kbe.player().chip then
        game.toast.show("chip不足");
        return;
    end

    self.insure_root:SetActive(false)
    game.kbe.player():insure();
    game.store.ingame.insure(true);
end

function actions:on_click_not_insure()
    game.store.ingame.insure(false);
    self.insure_root:SetActive(false)
end

-- 买保险阶段
function actions:on_insure(countdown)
    self.insure_root:SetActive(true);
    local price = math.floor(game.store.ingame.state.bet_value * 0.5)
    self.tip_txt.text = string.format("%s:%s", game.i18n.text("blackjack:保费"), game.i18n.number(price, "chip3"));
end

function actions:on_actions(actions)
    self.action_root:SetActive(true);
    self.action_1.gameObject:SetActive(false)
    self.action_2.gameObject:SetActive(false)
    self.action_3.gameObject:SetActive(false)
    self.action_4.gameObject:SetActive(false)
    for _, id in pairs(actions) do
        self["action_"..id].gameObject:SetActive(true)
    end
end

return actions